package com.skzh.web.controller.system;

import com.skzh.common.annotation.Log;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.page.TableDataInfo;
import com.skzh.common.enums.BusinessType;
import com.skzh.common.utils.poi.ExcelUtil;
import com.skzh.system.domain.BaseAttach;
import com.skzh.system.service.IBaseAttachService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件存储Controller
 *
 * @author wenlinchuan
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/system/attach")
public class BaseAttachController extends BaseController {
    @Autowired
    private IBaseAttachService baseAttachService;

    /**
     * 查询附件存储列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseAttach baseAttach) {
        startPage();
        List<BaseAttach> list = baseAttachService.selectBaseAttachList(baseAttach);
        return getDataTable(list);
    }

    /**
     * 导出附件存储列表
     */
    @Log(title = "附件存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseAttach baseAttach) {
        List<BaseAttach> list = baseAttachService.selectBaseAttachList(baseAttach);
        ExcelUtil<BaseAttach> util = new ExcelUtil<BaseAttach>(BaseAttach.class);
        util.exportExcel(response, list, "附件存储数据");
    }

    /**
     * 获取附件存储详细信息
     */
    @GetMapping(value = "/{attachId}")
    public AjaxResult getInfo(@PathVariable("attachId") Long attachId) {
        return success(baseAttachService.selectBaseAttachByAttachId(attachId));
    }

    /**
     * 新增附件存储
     */
    @Log(title = "附件存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseAttach baseAttach) {
        return toAjax(baseAttachService.insertBaseAttach(baseAttach));
    }

    /**
     * 修改附件存储
     */
    @Log(title = "附件存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseAttach baseAttach) {
        return toAjax(baseAttachService.updateBaseAttach(baseAttach));
    }

    /**
     * 删除附件存储
     */
    @Log(title = "附件存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attachIds}")
    public AjaxResult remove(@PathVariable Long[] attachIds) {
        return toAjax(baseAttachService.deleteBaseAttachByAttachIds(attachIds));
    }

    /**
     * 更新附件，有则更新，没有新建 --- 确保所有附件都存在attachBizTypeDict和attachBizId
     *
     * @param list 附件list
     */
    @ApiOperation(value = "更新附件list，存在则更新，不存在则插入")
    @PostMapping(value = "/saveAttachList")
    public AjaxResult saveAttachList(@RequestBody List<BaseAttach> list) {
        baseAttachService.addOrUpdateAttachList(list);
        return AjaxResult.success();
    }

    /**
     * 根据业务ID、业务类型查询附件list
     *
     * @param bizId   业务主键
     * @param bizType 业务类型
     * @return List<Attach>
     */
    @GetMapping(value = "/getAttachList")
    public AjaxResult getAttachList(String bizId, String bizType) {
        return AjaxResult.success(baseAttachService.getAttachList(bizId, bizType));
    }

    @ApiOperation(value = "根据bizId和bizType更新附件，存在则更新，不存在则插入")
    @PostMapping(value = "/saveAttachListWithBizId")
    public AjaxResult updateBaseAttaches(@RequestParam("bizId") String bizId, @RequestParam("bizType") String bizType,
                                         @RequestBody List<BaseAttach> list) {
        baseAttachService.updateBaseAttaches(bizId, bizType, list);
        return AjaxResult.success();
    }

}
